<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加与搜索商品 -->
      <el-row>
        <el-col>
          <el-input placeholder="请输入内容" v-model="goodsinfo.query" class="input-with-select" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
          <el-button type="primary" @click="openAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table border stripe :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="810"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="200"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="150"> </el-table-column>
        <el-table-column label="创建时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatTimestamp }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteGoods(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="goodsinfo.pagenum" :page-sizes="[30, 50, 100, 120]" :page-size="goodsinfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
  </div>
</template>

<script>
import commonAPI from '@/api/commonAPI/commonAPI.js'
import { clearDialog } from '@/mixin.js'
export default {
  name: 'GoodsList',
  mixins: [clearDialog],
  data() {
    return {
      goodsinfo: {
        query: '', // 查询的参数
        pagenum: 1, // 当前页
        pagesize: 5 // 每页显示的数据条数
      },
      // 商品总条数
      total: 0,
      // 商品列表
      goodsList: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 请求商品列表
    async getGoodsList() {
      const { data: res } = await commonAPI({ method: 'get', url: 'goods', params: this.goodsinfo })
      if (res.meta.status !== 200) return this.$message.error('请求商品数据失败!')
      this.$message.success('请求商品数据成功!')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 当改变每页显示多少条数据时触发
    handleSizeChange(size) {
      this.goodsinfo.pagesize = size
      this.getGoodsList()
    },
    // 当页码值时触发
    handleCurrentChange(num) {
      this.goodsinfo.pagenum = num
      this.getGoodsList()
    },
    // 点击删除按钮时触发
    deleteGoods(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async _ => {
          const { data: res } = await commonAPI({ method: 'delete', url: `goods/${id}` })
          if (res.meta.status !== 200) return this.$message.error('删除该商品失败')
          this.$message({ type: 'success', message: '删除成功!' })
          // 重新发起请求
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击添加商品的按钮时触发
    openAddGoods() {
      // 通过编程式导航跳转到添加商品的组件
      this.$router.push({ name: 'add' })
    }
  }
}
</script>

<style lang="less" scoped>
.input-with-select {
  width: 25%;
  margin-right: 15px;
}
.el-row {
  margin-bottom: 20px;
}
</style>
